package com.lind.kickoffki.android.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

import com.lind.kickoffki.android.GameEngine;
import com.lind.kickoffki.android.PaintThread;
import com.lind.kickoffki.android.R;




public class GameEngineView extends SurfaceView implements SurfaceHolder.Callback
{
    //used to keep track of time between updates and amount of time to sleep for
    long lastUpdate = 0;
    long sleepTime=0;
    //Game engine
    GameEngine gEngine;
    public Rect drawingRect = new Rect();
	
    //objects which house info about the screen
    SurfaceHolder surfaceHolder;
 
    //our Thread class which houses the game loop
    private PaintThread thread;
    TextView moveText, zeitText;
 
    //initialization code
    void initView(){
      //initialize our screen holder
      SurfaceHolder holder = getHolder();
      holder.addCallback( this);
      
      //initialize our game engine
      gEngine = new GameEngine(this);
      
      //initialize our Thread class. A call will be made to start it later
      thread = new PaintThread(holder, getContext(), new Handler(), gEngine);
      setFocusable(true);
   }
    
   
 
   //class constructors
    public GameEngineView(Context contextS, AttributeSet attrs){
        this(contextS, attrs, 0);
    }
    public GameEngineView(Context contextS, AttributeSet attrs, int defStyle){
       super(contextS, attrs, defStyle);
       initView();
   }
   
   void setAttrs(AttributeSet attr){
	   
   }

    //these methods are overridden from the SurfaceView super class. They are automatically called 
    // when a SurfaceView is created, resumed or suspended.
    @Override 
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}

    @Override 
    public void surfaceDestroyed(SurfaceHolder arg0) {
        boolean retry = true;
        //code to end gameloop
        thread.state=PaintThread.PAUSED;
        while (retry) {
            try {
                //code to kill Thread
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }

    }

    @Override 
    public void surfaceCreated(SurfaceHolder arg0) {
        if(thread.state==PaintThread.PAUSED){
            //When game is opened again in the Android OS
            thread = new PaintThread(getHolder(), getContext(), new Handler(), gEngine);
            thread.start();
        }else{
            //creating the game Thread for the first time
            thread.start();
        }
    }
    
    @Override
	public boolean onTouchEvent(MotionEvent event) {
		gEngine.steuerung.touched(event);
		return super.onTouchEvent(event);
	}
    
    public TextView getMoveText() {
		if (moveText == null) {
			moveText = (TextView) ((Activity) getContext())
					.findViewById(R.id.player_move_textview);
		}
		return moveText;
	}
	
	public TextView getZeitText() {
		if (zeitText == null) {
			zeitText = (TextView) ((Activity) getContext())
					.findViewById(R.id.time_textview);
		}
		return zeitText;
	}
	
	public void updateMoveText(final int resId){
		getHandler().post(new Runnable() {
			@Override
			public void run() {
				getMoveText().setText(resId);
				getMoveText().invalidate();
			}
		});
	}
	
	// zeigt die bis jetzt abgelaufene Zeit an
	public void showZeit(final long secs) {
		getHandler().post(new Runnable() {
			@Override
			public void run() {
				getZeitText().setText(
						getContext().getText(R.string.time)
								+ ": " + secs + "s");
				getZeitText().invalidate();
			}
		});
	}
	
	public Rect getDrawingRect(){
		getDrawingRect(drawingRect);
		return drawingRect;
	}
}